package lp.polimi.treefinder;

import java.util.ArrayList;
import java.util.List;

import lp.polimi.treefinder.utils.RankElement;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnShowListener;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

/**
 * After the server has sent the final ranking, this list is displayed to the user. Pushing on a leaf confirm the tag and
 * stores it in the tagged leaves XML file and in the tags list.
 */
public class RankingDialogFragment extends DialogFragment {

	private ArrayList<RankElement> ranking;
	
	private RankingDialogListener mListener;
	private int selectedLeaf;
	private LeafRankingAdapter adapter;
	private List<ImageView> obj;
	private AlertDialog dialog;
	
	private static final int UNSELECTED_COLOR = R.color.almost_white;
	private static final int SELECTED_COLOR = R.drawable.green_gradient;
	
	
	public static DialogFragment newInstance(ArrayList<RankElement> ranking) {
		DialogFragment fragment = new RankingDialogFragment();
		Bundle args = new Bundle();
		args.putParcelableArrayList("ranking", ranking);
		fragment.setArguments(args);
		return fragment;
	}
	
	
	public RankingDialogFragment() {
		// Required empty public constructor
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ranking=getArguments().getParcelableArrayList("ranking");
		adapter = new LeafRankingAdapter(getActivity(), R.layout.fragment_ranking_list, ranking);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		builder.setTitle(R.string.ranking_choice_title);
		builder.setSingleChoiceItems(adapter, 0, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialogInt, int which) {
				selectedLeaf = which;
				dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
				for (int i = 0; i < obj.size(); i++) {
					obj.get(i).setBackgroundColor(getResources().getColor(UNSELECTED_COLOR));
				}
				obj.get(which).setBackgroundDrawable(getResources().getDrawable(SELECTED_COLOR));
			}
		});
		
		builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialogInt, int id) {
				mListener.onRankingDialogPositiveClick(selectedLeaf);
			}
		});
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialogInt, int id) {
				mListener.onRankingDialogNegativeClick();
			}
		});
		dialog = builder.create();
		dialog.setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialogInt) {
				dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
			}
		});
		return dialog;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	public interface RankingDialogListener {
		public void onRankingDialogPositiveClick(int choice);
		public void onRankingDialogNegativeClick();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (RankingDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity+" must implement RankingDialogListener");
		}
	}
	
	
	private class LeafRankingAdapter extends ArrayAdapter<RankElement> {


		public LeafRankingAdapter(Context context, int resource, List<RankElement> objects) {
			super(context, resource, objects);
			obj=new ArrayList<ImageView>();
			for (int i = 0; i < objects.size(); i++) {
				ImageView imageView = (ImageView) getActivity().getLayoutInflater().inflate(resource, null);
				byte[] image = objects.get(i).getPhoto();
				imageView.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
				imageView.setBackgroundColor(getResources().getColor(UNSELECTED_COLOR));
				obj.add(imageView);
			}
		}

		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return obj.get(position); 
		}
	}
	
}
